/**
 * 
 */
package com.zc.BankCreditManagement.Servlet;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zc.BankCreditManagement.JavaBean.CustomerCreditLoan;
import com.zc.BankCreditManagement.JavaBean.DepositDetails;
import com.zc.BankCreditManagement.ServiceImpl.CustomerCreditLoanServiceImpl;
import com.zc.BankCreditManagement.ServiceImpl.DepositDetailsServiceImpl;

/**
 * @author ZhangCheng
 * @time 2019年9月23日
 */
@WebServlet("/CusLoanServlet")
public class CusLoanServlet extends HttpServlet {

	/**
	 * 一个特定标识符的序列号
	 */
	private static final long serialVersionUID = -8587072648885976147L;

	public CusLoanServlet() {
		super();
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String type = req.getParameter("Type");
		try {
			if ("LoanTypeCheck".equals(type)) {
				loanTypeCheck(req, resp);
			} else if ("CreditOnly".equals(type)) {
				resp.getWriter().write(cusCreditOnly(req, resp));
			} else if ("DepositOnly".equals(type)) {
				resp.getWriter().write(cusDepositOnly(req, resp));
			} else if ("Both".equals(type)) {
				cusBoth(req, resp);
			} else if ("FindCredit".equals(type)) {
				sendMsgToHTML(resp, cusFindCreditLoaning(req, resp));
			} else if ("FindDeposit".equals(type)) {
				sendMsgToHTML(resp, cusFindDeposittLoaning(req, resp));
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resp.getWriter().write(e.getMessage());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resp.getWriter().write(e.getMessage());
		}
	}

	/**
	 * 根据请求Id来获取用户的正在进行的所有正在进行的质押物贷款
	 * 
	 * @return ArrayList->DepositDetails
	 */
	private ArrayList<DepositDetails> cusFindDeposittLoaning(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		int userId = Integer.parseInt(req.getParameter("peoId"));
		DepositDetailsServiceImpl dDSI = new DepositDetailsServiceImpl();

		return dDSI.getAllLoaningDepositsById(userId);

	}

	/**
	 * 根据请求Id来获取用户的正在进行的信用贷款
	 * 
	 * @return CustomerCreditLoan
	 */
	private CustomerCreditLoan cusFindCreditLoaning(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		int userId = Integer.parseInt(req.getParameter("peoId"));
		CustomerCreditLoanServiceImpl cCLoanSI = new CustomerCreditLoanServiceImpl();

		return cCLoanSI.getLoaningById(userId);

	}

	/**
	 * 组合贷 根据请求Id来以及相关信息分别调用信用贷和质押物贷的方法
	 * 
	 * @throws ParseException
	 * @throws NumberFormatException
	 * 
	 */
	private void cusBoth(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException, NumberFormatException, ParseException {
		String de = cusDepositOnly(req, resp);
		String cre = cusCreditOnly(req, resp);

		CustomerCreditLoanServiceImpl cCLoanSI = new CustomerCreditLoanServiceImpl();

		resp.getWriter().write(cCLoanSI.combineCreditAndDeposit(de, cre));
	}

	/**
	 * 质押物贷款 根据请求Id以及质押物名称，质押物数量，质押物预估价格来获取质押物贷款操作
	 * 
	 * @return 字符串 Max "贷款上限达到最大值" null "插入失败" "" + manId "客户所被分配的客户经理"
	 * @throws ParseException
	 * @throws NumberFormatException
	 */
	public String cusDepositOnly(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException, NumberFormatException, ParseException {
		int cusId = Integer.parseInt(req.getParameter("peoId"));
		String cusWantLoanDeposit = req.getParameter("CusWantLoanDeposit");
		String cusWantLoanDepositNum = req.getParameter("CusWantLoanDepositNum");
		String cusWantLoanDepositPrice = req.getParameter("CusWantLoanDepositPrice");
		DepositDetailsServiceImpl dDSI = new DepositDetailsServiceImpl();

		return dDSI.cusDepositDetailsByCusId(cusId, cusWantLoanDeposit, cusWantLoanDepositNum, cusWantLoanDepositPrice);
	}

	/**
	 * 信用贷款 根据请求Id以及信用贷款额来获取信用贷款操作
	 * 
	 * @return 字符串 null have "空 有正在进行的贷款" null null "空 插入失败" "" + cusWantNum
	 *         "客户所需的期望贷款值"
	 * @throws ParseException
	 * @throws NumberFormatException
	 */
	private String cusCreditOnly(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException, NumberFormatException, ParseException {
		int cusId = Integer.parseInt(req.getParameter("peoId"));
		String cusWantNum = req.getParameter("CusWantNum");
		CustomerCreditLoanServiceImpl cCLoanSI = new CustomerCreditLoanServiceImpl();

		return cCLoanSI.cusCreditLoaningByCusId(cusId, Integer.parseInt(cusWantNum));
	}

	/**
	 * 贷款类型以及额度的查询
	 * 
	 * @return null null 信用等级不够 质押物贷款和信用贷款均不允许 null Deposit 信用等级仅可以进行质押物贷款 不足以进行信用贷款
	 *         Level + Money 信用贷款和质押物贷款均可以实现 Money为当前信用等级可以最高信用贷额度
	 */
	private void loanTypeCheck(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		int cusId = Integer.parseInt(req.getParameter("peoId"));
		CustomerCreditLoanServiceImpl cCLoanSI = new CustomerCreditLoanServiceImpl();

		resp.getWriter().write(cCLoanSI.getCusLoanType(cusId) + "");
	}

	/**
	 * 将对象包装成json格式给前端发送相应
	 */
	private void sendMsgToHTML(HttpServletResponse resp, Object obj) throws ServletException, IOException {
		Gson gson = new Gson();
		String msg = gson.toJson(obj);
		resp.setContentType("json/application;charset=UTF-8");
		resp.getWriter().write(msg);
	}
}
